import { AfterViewInit, Component, Input, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import * as _ from 'lodash';
import { CommonService } from '../common.service';
import { CommonDetail, CommonDialog, Detail, Dialog, FieldModule } from '../model/commom.model';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.scss']
})
/**
 * 描述：详情界面公共组件TS
 * 公司：xxx有限公司
 * 版权：xxx2020
 * 作者：oyjl
 * 时间：2020-04-27 08:26:38
 */
export class DetailComponent implements OnInit, AfterViewInit {

  @Input() commonDetail: CommonDetail; // 详情配置
  public commonDialog: CommonDialog; // 弹框配置
  public detail: Detail; // 详情配置
  public data: any; // 界面全部数据
  public showDialog: boolean; // 控制弹框显示隐藏
  public id: string; // 详情页界面id
  public showLoading: boolean; // 显示加载动画
  @ViewChild('content') content;
  constructor(
    public commonService: CommonService,
    public router: Router,
    public activatedRoute: ActivatedRoute) {
    this.activatedRoute.queryParams.subscribe(data => {
      this.id = data.id;
    });
    this.showLoading = false;
  }

  ngOnInit() {
    this.detail = this.commonDetail.detail;
    // 根据ID条件拼接
    if (this.detail.req) {
      const innerKey = Object.keys(this.detail.req || {});
      this.detail.req[this.detail.id] = this.id;
      innerKey.forEach(element => {
        if (this.detail.req[element] instanceof Object) {
          this.detail.req[element][this.detail.id] = this.id;
          delete this.detail.req[this.detail.id];
        }
      });
    }
    this.sendAllQequest(this.detail.url, this.detail.req);
  }

  ngAfterViewInit(): void {
    if (this.detail.buttons && this.detail.buttons.length > 0) {
      this.content.nativeElement.style.height = 'calc(100vh - 120px)';
    }
  }

  /**
   * 描述：发送单个请求获取数据
   * 作者：oyjl
   * 时间：2020-04-26 17:53:55
   */
  public sendQequest(module: FieldModule) {
    if (module.isRequest) {
      const url = module.url;
      // 根据ID条件拼接
      if (module.req) {
        // TODO：拼接请购单数组结构
        if (module.req.hasOwnProperty('requisitionIds')) {
          module.req.requisitionIds = this.data.requisitionId.split(',');
        }
        const innerKey = Object.keys(this.detail.req || {});
        if (innerKey.length > 1) {
          module.req[this.detail.id] = this.id;
        } else if (innerKey.length === 1 && module.req[innerKey[0]]) {
          module.req[innerKey[0]][this.detail.id] = this.id;
        } else { }
      }
      const req = module.req;
      this.commonService.getData(url, req, module.resKey).subscribe((res: any) => {
        module.data = res;
      });
    }
  }
  /**
   * 描述：一次性请求获取数据
   * 作者：oyjl
   * 时间：2020-04-27 09:48:43
   */
  public sendAllQequest(url, req) {
    this.showLoading = true;
    this.commonService.getData(url, req, this.detail.resKey).subscribe((res: any) => {
      this.data = res;
      this.commonService.cacheData = res;
      this.detail.modules.forEach(module => {
        let tempData = _.cloneDeep(res);
        if (module.resKey) {
          const arr = module.resKey.split('.');
          for (const i of arr) {
            tempData = tempData[i];
          }
          this.data.list = tempData;
        }
        this.sendQequest(module);
      });
      this.showLoading = false;
    });
  }

  /**
   * 描述：按钮点击控制
   * 作者：oyjl
   * 时间：2020-04-27 12:29:09
   */
  public buttonHandle(dialog: Dialog) {
    this.commonDialog = new CommonDialog(dialog);
    this.showDialog = true;
  }

  /**
   * 描述：返回上一历史界面
   * 作者：oyjl
   * 时间：2020-04-27 17:00:23
   */
  public goBack() {
    history.back();
  }

  /**
   * 描述：返回首页
   * 作者：oyjl
   * 时间：2020-04-28 09:05:40
   */
  public goHome() {
    this.router.navigateByUrl('/mobile/home');
  }

  /**
   * 描述：查看详情
   * 作者：oyjl
   * 时间：2020-04-27 17:36:31
   */
  public detailHandle(url, id) {
    this.router.navigateByUrl(url);
    this.router.navigate([url], { queryParams: { id } });
  }

  /**
   * 描述：获取内部数据，比如a.b,即是获取b数据
   * 作者：oyjl
   * 时间：2020-04-28 14:09:32
   */
  public getInnerValue(data = {}, key) {
    const tempKeys = key.split('.');
    for (const item of tempKeys) {
      if (data) {
        data = data[item];
      }
    }
    return data;
  }

  /**
   * 描述：合计处理
   * 作者：oyjl
   * 时间：2020-04-29 15:29:40
   */
  public totalHandle(data = [], fields) {
    let totals = 0;
    data.forEach(row => {
      let total = 1;
      fields.forEach(element => {
        total = total * Number(this.getInnerValue(row, element));
      });
      totals += total;
    });
    if (isNaN(totals)) {
      totals = 0;
    }
    return totals;
  }
}
